package com.henu.acmrating.crawler;

import com.henu.acmrating.pojo.RankItem;
import com.henu.acmrating.pojo.WeekStar;
import com.henu.acmrating.service.ProblemService;
import com.henu.acmrating.service.WeekStarService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
public class WeekStarScheduled {

    @Resource
    ProblemService problemService;

    @Resource
    WeekStarService weekStarService;

    private WeekStarScheduled weekStarScheduled;
    @PostConstruct
    public void init() {
        weekStarScheduled = this;
    }

    @Scheduled(cron = "0 10 4 ? * MON")
    public void start() {
        List<RankItem> rankItems = weekStarScheduled.problemService.showRank(7, 2);
        if(rankItems.size() > 0) {
            RankItem rankItem = rankItems.get(0);
            WeekStar weekStar = new WeekStar(rankItem.getUserName(), new Date(System.currentTimeMillis()));
            weekStarScheduled.weekStarService.insert(weekStar);
        }
    }
}
